package com.avic.rong.questionOrAnswer.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.network.Constant;
import com.avic.rong.questionOrAnswer.api.AddAnswerCommentApi;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.StringUtil;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.HashMap;

/**
 * 发布评论
 * created by xtj on 2018/4/16 16:26.
 */
public class PublishCommentActivity extends RxAppCompatActivity {
    private String contentId = "";//内容id
    private String contentType = "";//内容类型（1、图文（图文的健康知识）；2、专栏； 3、直播；4、广告；5、视频；6、音频；7、专家；8、医院；9、帖子与讨论；10、服务；11、机构）
    private TextView tvCommit, tvCancle;
    private EditText etContent;
    private LinearLayout llParent;
    public static final int RESULT_COMMENT_CODE = 1001;
    protected Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_comment);
        if (getIntent().getBundleExtra(Constant.START_ACTIVITY_PUTEXTRA) != null) {
            bundle = getIntent().getBundleExtra(Constant.START_ACTIVITY_PUTEXTRA);
        }
        if (bundle != null) {
            if (!StringUtil.isEmpty(bundle.getString("contentId"))) {
                contentId = bundle.getString("contentId");
            }
            if (!StringUtil.isEmpty(bundle.getString("contentType"))) {
                contentType = bundle.getString("contentType");
            }
        }
        tvCommit = findViewById(R.id.tv_commit);
        tvCancle = findViewById(R.id.tv_cancle);
        etContent = findViewById(R.id.et_content);
        llParent = findViewById(R.id.ll_parent);

        addListener();
    }

    private void addListener() {
        llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtil.isContainEmoji(PublishCommentActivity.this, etContent.getText().toString())) {
                    etContent.setText(StringUtil.replaceEmoji(etContent.getText().toString()));
                    etContent.setSelection(etContent.getText().toString().length());
                }
            }
        });
        tvCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.isEmpty(etContent.getText().toString().trim())) {
                    ToastTools.showToast(PublishCommentActivity.this, "请输入评论内容");
                    return;
                }
                sendComment();
            }
        });
    }

  /*  @Override
    protected int getContentViewId() {
        return R.layout.activity_publish_comment;
    }

    @Override
    protected void initView() {
        if (bundle != null) {
            if (!StringUtil.isEmpty(bundle.getString("contentId"))) {
                contentId = bundle.getString("contentId");
            }
            if (!StringUtil.isEmpty(bundle.getString("contentType"))) {
                contentType = bundle.getString("contentType");
            }
        }
        tvCommit = findViewById(R.id.tv_commit);
        tvCancle = findViewById(R.id.tv_cancle);
        etContent = findViewById(R.id.et_content);
        llParent = findViewById(R.id.ll_parent);
    }

    @Override
    protected void addListener() {
        llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtil.isContainEmoji(context, etContent.getText().toString())) {
                    etContent.setText(StringUtil.replaceEmoji(etContent.getText().toString()));
                    etContent.setSelection(etContent.getText().toString().length());
                }
            }
        });
        tvCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.isEmpty(etContent.getText().toString().trim())) {
                    ToastTools.showToast(context, "请输入评论内容");
                    return;
                }
                sendComment();
            }
        });
    }*/

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.bottom_out);
    }

    private void sendComment() {

        if (!SharedPreferencesUtil.getInstance(this).getLoginToken().isEmpty()) {
            HashMap<String, String> bodyMap = new HashMap<String, String>();
            bodyMap.put("ansId", contentId);
            bodyMap.put("commentType", contentType);
            bodyMap.put("comments", etContent.getText().toString().trim());
            bodyMap.put("token", SharedPreferencesUtil.getInstance(this).getLoginToken());
            final AddAnswerCommentApi addAnswerCommentApi = new AddAnswerCommentApi(PublishCommentActivity.this, simpleOnNextListener);
            addAnswerCommentApi.setCache(false);
            addAnswerCommentApi.setMap(bodyMap);
            HttpManager.getInstance().doHttpDeal(addAnswerCommentApi);
        }
    }

    //添加回答评论
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                ToastTools.showToast(PublishCommentActivity.this,subjects.getMsg());
                return;
            }
            ToastTools.showToast(PublishCommentActivity.this, "评论成功");
            setResult(RESULT_COMMENT_CODE);
            finish();

        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx",e.getMessage());

        }
    };
}
